package com.terren.spider.core.html.scheme.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.terren.spider.entity.common.PageData;
import com.terren.spider.entity.core.Entry;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.utils.UrlUtils;

public class LJErshoufangScheme  extends BasicCustomScheme{


	@Override
	public Set<String> findHelpPaginationUrl(Page page, Entry entry) {
		Set<String> resultSet = new HashSet<>();
		resultSet.addAll(findPagination(page, entry)); // 分页部分
		return resultSet;
	}

	/**
	 * 分页
	 * @param page
	 * @param entry
	 * @return
	 */
	private Set<String> findPagination(Page page, Entry entry) {
		List<String> pgList = new ArrayList<String>();// 存放分页list
		String pageUrl = "";
		String pageDataStr = "";
		List<String> pageUrls = page.getHtml().$("div.page-box", "page-url").all();
		List<String> pageDataStrs = page.getHtml().$("div.page-box", "page-data").all();
		if (pageUrls != null && pageUrls.size() >= 2) {
			pageUrl = pageUrls.get(1);
		}
		if (pageDataStrs != null && pageDataStrs.size() >= 2) {
			pageDataStr = pageDataStrs.get(1);
		}
		if (!"".equals(pageDataStr) && !"".equals(pageUrl)) {
			PageData pageData = new PageData();
			Gson g = new Gson();
			List<PageData> list = g.fromJson("[" + pageDataStr + "]", new TypeToken<List<PageData>>() {
			}.getType());
			pageData = list.get(0);
			String domain = UrlUtils.getDomain(entry.getEntryUrl());
			for (int i = 2; i <= pageData.getTotalPage(); i++) {
				
				pgList.add("https://"+domain + pageUrl.replace("{page}", i + ""));
			}
		}
		Set<String> resultSet = new HashSet<>();
		resultSet.addAll(pgList);
		resultSet.remove(null);
		return resultSet;
	}
}
